package com.jkj.http.controller;

import com.jkj.http.entity.LayChildNavbar;
import com.jkj.http.entity.LayNavbar;
import com.jkj.http.entity.ModuleType;
import com.jkj.http.entity.Operator;
import com.jkj.http.interceptor.LoginChecked;
import com.jkj.http.sys.entity.SysPermission;
import com.jkj.http.sys.service.PermissionService;
import com.jkj.utils.JsonUtils;
import com.jkj.utils.OperatorProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/home")
public class HomeController extends BaseController {
    @Autowired
    private PermissionService permissionService;

    @Value("${softwareName}")
    private String softwareName;
    /**
     * 后台首页视图
     *
     * @param request
     * @param session
     * @return
     */
    @LoginChecked()
    @RequestMapping(value = "/index", method = {RequestMethod.GET})
    public ModelAndView index(HttpServletRequest request, HttpSession session) {
        Operator operator = OperatorProvider.getCurrent(session);
        if (operator != null) {
            ModelAndView view = new ModelAndView();
            view.addObject("SoftwareName", softwareName);
            view.addObject("Account", operator.getAccount());
            view.addObject("Avatar", operator.getAvatar());
            view.setViewName("system/home/index");
            return view;
        } else {
            return new ModelAndView("redirect:/account/login");
        }
    }

    /**
     * 默认显示视图
     *
     * @return
     */
    @RequestMapping(value = "/default", method = {RequestMethod.GET})
    public ModelAndView defaultView() {
        return new ModelAndView("system/home/default");
    }

    /**
     * 获取左侧菜单
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getLeftMenu", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String getLeftMenu(HttpServletRequest request) {
        String userId = OperatorProvider.getCurrent(request.getSession()).getUserId();
        String projectName = request.getContextPath();
        List<LayNavbar> listNavbar = new ArrayList<LayNavbar>();
        List<SysPermission> listModules = permissionService.getList(userId);
        for (SysPermission item : listModules) {
            if (item.getType() == ModuleType.Menu && item.getLayer() == 0) {
                LayNavbar navbarEntity = new LayNavbar();
                List<LayChildNavbar> listChildNav = getChildNavList(projectName, listModules, item.getId());
                navbarEntity.setIcon(item.getIcon());
                navbarEntity.setSpread(false);
                navbarEntity.setTitle(item.getName());
                navbarEntity.setChildren(listChildNav);
                listNavbar.add(navbarEntity);
            }
        }
        return JsonUtils.toJSON(listNavbar);
    }

    private List<LayChildNavbar> getChildNavList(String projectName, List<SysPermission> listModules, String parentId) {
        List<LayChildNavbar> list = new ArrayList<LayChildNavbar>();
        for (SysPermission item : listModules) {
            if (item.getType() == ModuleType.Menu && item.getLayer() == 1 && item.getParentId().equals(parentId)) {
                LayChildNavbar childNavbar = new LayChildNavbar();
                childNavbar.setHref(projectName + item.getUrl());
                childNavbar.setTitle(item.getName());
                childNavbar.setIcon(item.getIcon());
                list.add(childNavbar);
            }
        }
        return list;
    }

    /**
     * 获取登录用户权限
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/getPermission", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String getPermission(HttpSession session) {
        String userId = OperatorProvider.getCurrent(session).getUserId();
        List<SysPermission> listModules = permissionService.getList(userId);
        return JsonUtils.toJSON(listModules);
    }
}
